/**
 * Created by Administrator on 2016/1/25.
 */
require.config(require_config);
define([
    'jquery',
    'common',
    'ready',
    'template',
    'collection',
    'model',
    'dialog'
],function($, common, ready, template, Collection, Model, dialog){
    var index = function () {
        this.init();
    }

    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var that = this;
            that.collection = new Collection({
                url: _global.url.api + 'specification',
                condition:{offset:0,limit:10},
                model:{
                    url:{
                        put:_global.url.api + 'specification/{id}'
                    },
                    event:{
                        '.item-edit:click':'edit'
                    },
                    edit: function (e) {
                        e.preventDefault();
                        var _this = this;
                        dialog({
                            title:'编辑规格',
                            width:400,
                            content:template('edit-tpl',_this.data),
                            ok: function () {
                                _this.data.title = $('#title').val();
                                _this.data.is_required = $('[name="is_required"]:checked').val();

                                if(!_this.put()){
                                    return false;
                                }
                                that.collection.reload();
                            },cancel:function(){}
                        }).show();
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },
        event: function () {
            this.data = {};
            var _this = this;
            $('#add-specification').on('click', function (e) {
                e.preventDefault();
                dialog({
                    title:'添加规格参数',
                    width:400,
                    content : template('add-tpl'),
                    ok: function () {
                        var data = {
                            title:$("#title").val(),
                            name:$('#name').val(),
                            is_required:$('[name="is_required"]:checked').val()

                        };
                        if(!new Model({
                            url:_global.url.api + 'specification',
                            data:data,
                            valid_config: {
                                title:'required',
                                is_required:'required'
                            }
                        }).post()){
                            return false;
                        };
                        _this.fetch();
                    },cancel: function () {}
                }).show(e.target);
            });
            $('#choise-bat').on('click', function (e) {
                e.preventDefault();
                dialog({
                    title:'分配规格参数',
                    width:600,
                    content : template('set-spec'),
                    ok: function () {
                        var data = {
                            cid:$('textarea[name="cid"]').val(),
                            sid:$('textarea[name="sid"]').val()
                        };
                        if(!new Model({
                                url:_global.url.api + 'set_specification_bat',
                                data:data,
                                valid_config: {
                                    cid:'required',
                                    sid:'required'
                                }
                            }).post()){
                            return false;
                        };
                        _this.fetch();
                    },cancel: function () {}
                }).show(e.target);
            })
        }
    }

    new index();
})